package com.app.service.impl;

import com.app.mapper.jiuquan.IncubatorsMapper;
import com.app.mapper.jiuquan.SiteApplyForMapper;
import com.app.model.*;
import com.app.service.SiteApplyForService;


import com.app.util.StringTool;
import com.app.vo.SiteApplyForVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SiteApplyForServiceImpl extends ServiceImpl<SiteApplyForMapper, SiteApplyForModel> implements SiteApplyForService {

    @Autowired
    SiteApplyForMapper siteApplyForMapper;
    @Autowired
    IncubatorsMapper incubatorsMapper;

    @Override
    public SiteApplyForVo getListByModel(SiteApplyForModel model) {

        Page<SiteApplyForModel> page1 = new Page<>(model.getPage(), model.getLimit());
        QueryWrapper<SiteApplyForModel> qw = new QueryWrapper<>();
        if(StringTool.isNotEmpty(model.getRealName()))
        {
            qw.like("real_name", model.getRealName());
        }
        if(StringTool.isNotEmpty(model.getPhone()))
        {
            qw.like("phone", model.getPhone());
        }
        if(model.getState()!=null&&model.getState()>0)
        {
            qw.like("state", model.getState());
        }
        Page<SiteApplyForModel> siteApplyForModelPage = siteApplyForMapper.selectPage(page1, qw);
        List<SiteApplyForModel> siteApplyForModelList = siteApplyForModelPage.getRecords();
        long count = siteApplyForModelPage.getTotal();
        for (SiteApplyForModel siteApplyForModel : siteApplyForModelList) {

            IncubatorsModel incubatorsModel = incubatorsMapper.selectById(siteApplyForModel.getIncubatorsId());
            siteApplyForModel.setSiteName(incubatorsModel.getName());

        }
        SiteApplyForVo trainPeopleVo = new SiteApplyForVo();
        trainPeopleVo.setList(siteApplyForModelList);
        trainPeopleVo.setCount((int) count);
        return trainPeopleVo;
    }

    @Override
    public SiteApplyForVo getMyListByUserId(SiteApplyForModel model) {
        Page<SiteApplyForModel> page1 = new Page<>(model.getPage(), model.getLimit());
        QueryWrapper<SiteApplyForModel> qw = new QueryWrapper<>();
        qw.eq("user_id", model.getUserId());
        Page<SiteApplyForModel> siteApplyForModelPage = siteApplyForMapper.selectPage(page1, qw);
        List<SiteApplyForModel> siteApplyForModelList = siteApplyForModelPage.getRecords();
        for (SiteApplyForModel siteApplyForModel : siteApplyForModelList) {

            IncubatorsModel incubatorsModel = incubatorsMapper.selectById(siteApplyForModel.getIncubatorsId());
            siteApplyForModel.setSiteName(incubatorsModel.getName());

        }
        long count = siteApplyForModelPage.getTotal();
        SiteApplyForVo trainPeopleVo = new SiteApplyForVo();
        trainPeopleVo.setList(siteApplyForModelList);
        trainPeopleVo.setCount((int) count);
        return trainPeopleVo;
    }
}
